/* 
 * File:   testmain.cpp
 * Author: george
 *
 * Created on October 24, 2012, 11:50 AM
 */

#include <cstdlib>
#include "common.h"
#include "Scanner.h"
#include "Tokenizer.h"

using namespace std;

void testScanFile();
void testGetSourceLine();
void testSkipComment();
void testBackUpCharacter();
void testGetCharacter();

void testGetToken();
void testSkipBlanks();
void testGetWord();
void testGetNumber();
void testGetString();
void testGetSpecial();
void testIsResWord();

void testPrintPageHeader();

void testPrintLine();




/*
 * 
 */
int main(int argc, char** argv) 
{
    
    cout << "TESTING SCANNER FUNCTIONS... \n\n";
    
    testScanFile();
    cout << endl;
    testGetSourceLine();
    cout << endl;
    testSkipComment();
    cout << endl;
    testBackUpCharacter();
    cout << endl;
    testGetCharacter();
    cout << endl;
    
    cout << "TESTING TOKENIZER FUNCTIONS... \n\n";
    
    testGetToken();
    cout << endl;
   
    testSkipBlanks();
    cout << endl;
    
    testGetWord();
    cout << endl;
    testGetNumber();
    cout << endl;
    testGetString();
    cout << endl;
    testGetSpecial();
    cout << endl;
    testIsResWord();
    cout << endl;
    
    cout << "TESTING PRINT FUNCTIONS... \n\n";


    testPrintPageHeader();
    cout << endl;
    testPrintLine();
    
    
    
    
    
    
    
    //filename = "";
    //testScanner.sourceFile = fopen(filename.c_str(), "r");
    //cout << "Actual output: " << testScanner.GetSourceLine() << endl;
    
    return 0;
}

void testScanFile()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    Tokenizer testTok = Tokenizer(testScanner);
    
    
    cout << "Testing ScanFile(Tokenizer &)\t" << "Valid: testTok:\t" << "Expected output: run the whole program" << endl;
    cout << "Actual output: \n";
    testScanner.ScanFile(testTok);
    cout << "Invalid input will not compile because the function will not accept anything but a Tokenizer." << endl;
}

void testGetSourceLine()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    //Tokenizer testTok = Tokenizer(testScanner);
    string filename = "NEWTON.PAS";
    testScanner.sourceFile = fopen(filename.c_str(), "r");

    cout << "Testing GetSourceLine()\t Valid: name = NEWTON.PAS \n Expected output: true" << endl;
    cout << "Actual output: " << testScanner.GetSourceLine() << endl;
    cout << "Invalid: name != NEWTON.PAS\t" << "Will not compile" << endl;
}

void testSkipComment()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    //Tokenizer testTok = Tokenizer(testScanner);
    cout << "Testing SkipComment() with {Test String}:\t\t" << "Expected output: blank" << endl;
    cout << "Actual output:\t" << endl;
    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "{Test String}";
    testScanner.SkipComment();
    cout << "end testSkipComment()" << endl;
    
    
}


void testBackUpCharacter()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "Test String";
    
    cout << "Testing BackUpCharacter() with Test String. \t" << endl;
    cout << "Expected output:\t " << "T  ->   T"<< endl;
    cout << "Actual output:\t " << testScanner.GetCharacter() << "   ->   ";
    testScanner.BackUpCharacter();
    cout << testScanner.GetCharacter() << endl;
    
}

void testGetCharacter()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "Test String";
    
    cout << "Testing GetCharacter() with Test String " << endl;
    cout << "Expected output: T" << endl;
    cout << "Actual output : " << testScanner.GetCharacter() << endl;
    
    
}

void testGetToken()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    Tokenizer wordTok = Tokenizer(testScanner);
    
    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "Test String";
    cout << "Testing GetToken() with Test String" << endl << "Expected output : test" << endl;
    wordTok.GetToken(testScanner.charTable);
    cout << "Actual output: " << wordTok.tokenString << endl;
    cout << endl;
    
    Tokenizer numTok = Tokenizer(testScanner);
    
    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "2.542";
    cout << "Testing GetToken() with 2.542" << endl << "Expected output : 2.542" << endl;
    numTok.GetToken(testScanner.charTable);
    cout << "Actual output: " << numTok.tokenString << endl;
    cout << endl;
    Tokenizer specialTok = Tokenizer(testScanner);
    
    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "(";
    cout << "Testing GetToken() with (" << endl << "Expected output : (" << endl;
    specialTok.GetToken(testScanner.charTable);
    cout << "Actual output: " << specialTok.tokenString << endl;
    cout<< endl;
    Tokenizer stringTok = Tokenizer(testScanner);
    
    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "'Test String'";
    cout << "Testing GetToken() with 'Test String'" << endl << "Expected output : Test String" << endl;
    stringTok.GetToken(testScanner.charTable);
    cout << "Actual output: " << stringTok.tokenString << endl;
    cout << endl;
}
void testSkipBlanks()
{
    
    Scanner testScanner = Scanner("NEWTON.PAS");
    Tokenizer testTok = Tokenizer(testScanner);

    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "      string";
    cout << "Testing GetToken() with '      string'" << endl << "Expected output : s" << endl;
    testTok.SkipBlanks();
    
    cout << "Actual output: " << testScanner.GetCharacter() << endl;
    

}
void testGetWord()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    Tokenizer testTok = Tokenizer(testScanner);

    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "Test";
    cout << "Testing GetToken() with Test" << endl << "Expected output : Test" << endl;
    testTok.GetWord(testScanner.GetCharacter(), testScanner.charTable);
    
    cout << "Actual output: " << testTok.tokenString << endl;
}
void testGetNumber()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    Tokenizer tTok = Tokenizer(testScanner);

    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "2e6";
    cout << "Testing GetToken() with 2e6" << endl << "Expected output : 2e6" << endl;
    tTok.GetNumber('2', testScanner.charTable);
    
    cout << "Actual output: " << tTok.tokenString << endl;
}
void testGetString()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    Tokenizer testTok = Tokenizer(testScanner);

    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = "'Test String'";
    cout << "Testing GetToken() with 'Test String' " << endl << "Expected output : Test String" << endl;
    testTok.GetString(testScanner.GetCharacter());
    
    cout << "Actual output: " << testTok.tokenString << endl;
}

void testGetSpecial()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    Tokenizer testTok = Tokenizer(testScanner);

    testScanner.sourceFile = fopen("NEWTON.PAS", "r");
    testScanner.sourceLineIndex = 0;
    testScanner.sourceLine = ",";
    cout << "Testing GetToken() with , " << endl << "Expected output : ," << endl;
    testTok.GetSpecial(testScanner.GetCharacter());
    
    cout << "Actual output: " << testTok.tokenString << endl;
    
}
void testIsResWord()
{
    Scanner testScanner = Scanner("NEWTON.PAS");
    Tokenizer testTok = Tokenizer(testScanner);
    string str  = "word";
    cout << "Testing GetToken() with word " << endl << "Expected output: 0" << endl;
    testTok.GetSpecial(testScanner.GetCharacter());
    
    cout << "Actual output: " << testTok.IsReservedWord(str) << endl;
    
    
    
}

void testPrintLine()
{
    
    cout << "Testing PrintLine() with String\t" << "\nExpected output: Print page header, then print String " << endl;
    cout << "Actual output: ";
    Print testPrinter= Print("NEWTON.PAS");
    testPrinter.PrintLine("String");
    cout << endl;
    
}


void testPrintPageHeader()
{
    cout << "Testing PrintPageHeader()\t" << "\nExpected output: Page header printed " << endl;
    cout << "Actual output: ";
    Print printer = Print("NEWTON.PAS");
    printer.PrintPageHeader();
    
    
            
}